package com.mornd.gulimall.product.service.impl;

import com.mornd.gulimall.product.entity.SpuImages;
import com.mornd.gulimall.product.mapper.SpuImagesMapper;
import com.mornd.gulimall.product.service.ISpuImagesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * spu图片 服务实现类
 * </p>
 *
 * @author mornd
 * @since 2023-07-23
 */
@Service
public class SpuImagesServiceImpl extends ServiceImpl<SpuImagesMapper, SpuImages> implements ISpuImagesService {

    @Override
    public void saveImages(Long spuId, List<String> imgs) {
        List<SpuImages> saves = new ArrayList<>();
        int sort = 0;
        for (String img : imgs) {
            // todo url 校验
            SpuImages spuImages = new SpuImages();
            spuImages.setSpuId(spuId);
            spuImages.setImgUrl(img);
            spuImages.setImgSort(sort++);
            spuImages.setDefaultImg(0);
            saves.add(spuImages);
        }

        saves.get(0).setDefaultImg(1);
        super.saveBatch(saves);
    }
}
